#!/bin/sh
################################################################################
# simplest_tbf.qos (Simple TBF shaper)
#
# Abstract:
# This is a single band fq_codel and ipv6 enabled shaping script for Ethernet
# gateways. This is nearly the simplest possible. With FQ_CODEL, the sparseness
# priority will work pretty well for a casual network. Flow-hashes should not
# overlap much with only a few users.
#
# Uses TBF instead of HTB as that may give better performance on some
# architectures.
#
# References:
# This alternate shaper attempts to go for 1/u performance in a clever way
# http://git.coverfire.com/?p=linux-qos-scripts.git;a=blob;f=src-3tos.sh;hb=HEAD
#
################################################################################
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
#  Copyright (C) 2012-2017
#    Michael D. Taht, Toke Høiland-Jørgensen, Sebastian Moeller
#
################################################################################

. ${SQM_LIB_DIR}/defaults.sh

################################################################################

egress() {

    MTU=$(get_mtu $IFACE)
    BURST="$(get_burst ${MTU:-1514} ${UPLINK})"
    BURST=${BURST:-1514}

    $TC qdisc del dev $IFACE root 2>/dev/null

    $TC qdisc add dev $IFACE root handle 1: $(get_stab_string) tbf \
        rate ${UPLINK}kbit burst $BURST latency 300ms $(get_htb_adsll_string)
    $TC qdisc add dev $IFACE parent 1: handle 110: $QDISC \
        $(get_limit ${ELIMIT}) $(get_target "${ETARGET}" ${UPLINK}) \
        $(get_ecn ${EECN}) $(get_flows ${UPLINK}) ${EQDISC_OPTS}

}

ingress() {
    sqm_debug "ingress"
    $TC qdisc del dev $IFACE handle ffff: ingress 2>/dev/null
    $TC qdisc add dev $IFACE handle ffff: ingress

    MTU=$(get_mtu $IFACE)
    BURST="$(get_burst ${MTU:-1514} ${DOWNLINK})"
    BURST=${BURST:-1514}

    $TC qdisc del dev $DEV root 2>/dev/null

    $TC qdisc add dev $DEV root handle 1: $(get_stab_string) tbf \
        rate ${DOWNLINK}kbit burst $BURST latency 300ms $(get_htb_adsll_string)
    $TC qdisc add dev $DEV parent 1: handle 110: $QDISC \
        $(get_limit ${ILIMIT}) $(get_target "${ITARGET}" ${DOWNLINK}) \
        $(get_ecn ${IECN}) $(get_flows ${DOWNLINK}) ${IQDISC_OPTS}

    $IP link set dev $DEV up

    # redirect all IP packets arriving in $IFACE to ifb0

    $TC filter add dev $IFACE parent ffff: protocol all prio 10 u32 \
        match u32 0 0 flowid 1:1 action mirred egress redirect dev $DEV

}

sqm_start() {
    [ -n "$IFACE" ] || return 1
    do_modules
    verify_qdisc "tbf" || return 1

    case $QDISC in
        cake*)
            sqm_warn "Cake is not supported with this script; falling back to FQ-CoDel"
            QDISC=fq_codel ;;
    esac

    verify_qdisc $QDISC || return 1
    sqm_debug "Starting ${SCRIPT}"

    [ -z "$DEV" ] && DEV=$( get_ifb_for_if ${IFACE} )


    if [ "$UPLINK" -ne 0 ]; then
        egress
        sqm_debug "egress shaping activated"
    else
        sqm_debug "egress shaping deactivated"
        $TC qdisc del dev $IFACE root 2> /dev/null
    fi
    if [ "$DOWNLINK" -ne 0 ]; then
        verify_qdisc ingress "ingress" || return 1
        ingress
        sqm_debug "ingress shaping activated"
    else
        sqm_debug "ingress shaping deactivated"
        $TC qdisc del dev $DEV root 2> /dev/null
        $TC qdisc del dev $IFACE ingress 2> /dev/null
    fi

    return 0
}

################################################################################
